//
//  WmcEditWatermarkVC+Function.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/10.
//

import UIKit

extension WmcEditWatermarkVC {
    /// 刷新导航条按钮
    func reloadNavigationItem() {
        var defaultList: [WmcButtonItemType.EditWMNavigation] = []
        if YPWmcManager.shared.isIndependentPage {
            defaultList = [
                .shareWatermark,
            ]
        }
        if let wmModel = viewModel.editWatermarkModelRelay.value,
           let _defaultList = editWatermarkDelegate?.navigationRightMenuList(.current,
                                                                             watermarkModel: wmModel,
                                                                             defaultList: defaultList)
        {
            defaultList = _defaultList
        }
        let views = toNavigationItemList(caseList: defaultList)

        starkView.yp.arrangedSubviews = views
    }

    /// 导航条的按钮实例化
    func toNavigationItemList(caseList: [WmcButtonItemType.EditWMNavigation]) -> [UIView] {
        var views: [UIView] = []
        for cas in caseList {
            switch cas {
            case .shareWatermark: views.append(shareWatermarkBtn)
            case let .custom(view: view):
                views.append(view)
            }
        }
        return views
    }

    /// 刷新属性列表
    func reloadAttributeList(attList: [WmcWatermarkOptionModel],
                             watermarkModel: WmcWatermarkDetailModel) -> [WmcWatermarkOptionModel]
    {
        var newAttList: [WmcWatermarkOptionModel] = []
        var defaultList = attList.map { $0.fieldType }
        // 询问外部代理
        if let _defaultList = editWatermarkDelegate?.editAttributeList(.current,
                                                                       watermarkModel: watermarkModel,
                                                                       defaultList: defaultList)
        {
            defaultList = _defaultList
            attList.forEach { option in
                if defaultList.contains(option.fieldType) {
                    newAttList.append(option)
                }
            }
        } else { // 无代理，则直接赋值，不处理遍历优化
            newAttList = attList
        }
        return newAttList
    }

    /// 刷新基础列表
    func reloadBasicList(watermarkModel: WmcWatermarkDetailModel) -> [WmcEditWatermarkSettingCell.Model] {
        // 基础属性配置
        var defaultList: [WmcButtonItemType.EditWMBasicItem] = [
            .alpha, .size,
        ]
        // 询问外部代理
        if let _defaultList = editWatermarkDelegate?.editBasicList(.current, watermarkModel: watermarkModel, defaultList: defaultList) {
            defaultList = _defaultList
        }
        let basicModels = toBasicItemList(defaultList, watermarkModel: watermarkModel)
        return basicModels
    }

    /// 转换
    func toBasicItemList(_ caseList: [WmcButtonItemType.EditWMBasicItem],
                         watermarkModel: WmcWatermarkDetailModel) -> [WmcEditWatermarkSettingCell.Model]
    {
        var models: [WmcEditWatermarkSettingCell.Model] = []
        for cas in caseList {
            switch cas {
            case .alpha:
                let model = WmcEditWatermarkSettingCell.Model(settingType: .alpha, value: watermarkModel)
                models.append(model)
            case .size:
                let model = WmcEditWatermarkSettingCell.Model(settingType: .size, value: watermarkModel)
                models.append(model)
            }
        }
        return models
    }
}
